<?php

class KishuMapper extends DataMapper
{
    const MODEL_CLASS = 'Kishu';

    // ------------- 更新系クエリ -----------------
    function insert($data) {

        $pdo = $this->_pdo;
        $modelClass = static::MODEL_CLASS;

        $stmt = $pdo->prepare('
            INSERT INTO mst_service(jpn, eng)
            VALUES (?, ?)
        ');
        $stmt->bindParam(1, $jpn,  PDO::PARAM_STR);
        $stmt->bindParam(2, $eng,  PDO::PARAM_STR);

        if (! is_array($data)) {
            $data = array($data);
        }
        foreach ($data as $row) {
            if (! $row instanceof $modelClass || $row->isValid()) {
                throw new InvalidArgumentException;
            }
            $jpn  = $row->jpn;
            $eng  = $row->jpn;
            $stmt->execute();

            //autoincrementな主キーをオブジェクト側へ反映
            $row->id = $pdo->lastInsertId();
        }
    }

    function update($data)
    {
        $modelClass = static::MODEL_CLASS;

        $stmt = $this->_pdo->prepare('
            UPDATE mst_service
               SET jpn = ?
             WHERE id = ?
        ');
        $stmt->bindParam(1, $jpn,PDO::PARAM_STR);
        $stmt->bindParam(2, $id, PDO::PARAM_INT);

        if (! is_array($data)) {
            $data = array($data);
        }
        
        foreach ($data as $row) {
            if (! $row instanceof $modelClass || $row->isValid()) {
                throw new InvalidArgumentException;
            }
            $id  = $row->id;
            $jpn = $row->jpn;
            $stmt->execute();
        }
    }

    function delete($data)
    {
        $modelClass = static::MODEL_CLASS;

        $stmt = $this->_pdo->prepare('
            DELETE FROM mst_service
             WHERE id = ?
        ');
        $stmt->bindParam(1, $id, PDO::PARAM_INT);

        if (! is_array($data)) {
            $data = array($data);
        }
        foreach ($data as $row) {
            if (! $row instanceof $modelClass) {
                throw new InvalidArgumentException;
            }
            $id = $row->id;
            $result = $stmt->execute();
        }
    }

    //------------- 参照系クエリ ----------------

    function findById($entryId)
    {
        $stmt = $this->_pdo->prepare('
            SELECT *
              FROM mst_service
             WHERE id = ?
        ');
        $stmt->bindParam(1, $entryId, PDO::PARAM_INT);
        $stmt->execute();

        $this->_decorate($stmt);
        return $stmt->fetch();
    }

    function find(modelLoader $loader)
    {
        if ($loader->solo) {
            return $this->findById($loader->solo);
        }
        
        $order = $this->getOrderString($loader->order);
        $where = $this->getSearchString($loader->alike, 'ilike', 'jpn', $this->_pdo);

        $query = '
            SELECT id, jpn, upd
              FROM mst_service
              '.$where.'
              '.$order.'
        ';

        $stmt = $this->_pdo->query($query);
        $stmt->execute();

        return $this->_decorate($stmt, $query);
    }

    // TagかTagの配列から関連するEntryを引く
    // 配列で渡すとORとして取得
    function findByTag($tag)
    {
        $pdo = $this->_pdo;

        if (!is_array($tag)) {
            $tag = array($tag);
        }

        // INを使いたいのでプリペアドステートメントを使わず
        // 手動でSQLを組み立てる
        $taglist = array();
        foreach ($tag as $t) {
            if (! $t instanceof Tag || ! $t->isValid()) {
                throw new InvalidArgumentException;
            }
            $taglist[] = $pdo->quote($t->tag, PDO::PARAM_STR);
        }
        $stmt = $pdo->query('
            SELECT DISTINCT Entry.*
              FROM Entry NATURAL JOIN Tag
             WHERE Tag.tag IN (' . implode(',', $taglist) .')
        ');

        return $this->_decorate($stmt);
    }
}